package controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import model.Distr;
import model.Request;
import model.RequestModel;
import model.User;

import org.primefaces.event.SelectEvent;  
import org.primefaces.model.SelectableDataModel;

import tests.PopulateData;

import java.util.Date;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ApplicationScoped
@ManagedBean(name="requestsBean")
public class RequestsBean {
	
	private List<Request> requestsRaw = new ArrayList<Request>();
	
	private Request selectedRequest;
	
	private RequestModel requestProduction;
	
	private int indexSelectedRequest;
	
	public RequestsBean() {
			
		requestsRaw = PopulateData.populateRequestsData(10);
		
		requestProduction = new RequestModel(requestsRaw);
	}
	
	public int getIndexSelectedRequest() {
		return indexSelectedRequest;
	}
	
	public void setIndexSelectedRequest(int indexSelectedRequest) {
		this.indexSelectedRequest = indexSelectedRequest;
	}
	
	public Request getSelectedRequest() {
		return selectedRequest;
	}
	
	public void setSelectedRequest(Request request) {
		this.selectedRequest = request;
	}
	
	public Long getRequestsCount() {
		return (Long.valueOf(requestsRaw.size())); 
	}
	
	public RequestModel getRequestsTest() {
		return requestProduction;
	}
	
	public void onRowSelect() {
		ConfigurableNavigationHandler configurableNavigationHandler =
	             (ConfigurableNavigationHandler)FacesContext.
	               getCurrentInstance().getApplication().getNavigationHandler();
	       
	         configurableNavigationHandler.performNavigation("request?faces-redirect=true");
	         setIndexSelectedRequest(requestsRaw.indexOf(selectedRequest));
	}
	
	public String saveAndExit() {

		requestsRaw.remove(getIndexSelectedRequest());
		requestsRaw.add(getIndexSelectedRequest(), selectedRequest);
		requestProduction = new RequestModel(requestsRaw);
		return "main?faces-redirect=true";
	}
	
	public String save() {

		requestsRaw.remove(getIndexSelectedRequest());
		requestsRaw.add(getIndexSelectedRequest(), selectedRequest);
		requestProduction = new RequestModel(requestsRaw);
		return null;
	}
	
	public String exit() {
		return "main?faces-redirect=true";
	}
	
	// Kill it with fire!!!
	public int getSelectedRequestId() {
		return selectedRequest.getId();
	}
	
	public String getSelectedRequestName() {
		return selectedRequest.getName();
	}
	
	public User getSelectedRequestCustomer() {
		return selectedRequest.getCustomer();
	}
	
	public String getSelectedRequestCustomerFullname() {
		return selectedRequest.getCustomerFullname();
	}
	
	public User getSelectedRequestResponsible() {
		return selectedRequest.getResponsible();
	}
	
	public User getSelectedRequestAccountable() {
		return selectedRequest.getAccountable();
	}

	public User getSelectedRequestConsulted() {
		return selectedRequest.getConsulted();
	}
	
	public User getSelectedRequestInformed() {
		return selectedRequest.getInformed();
	}
	
	public User getSelectedRequestSupport() {
		return selectedRequest.getSupport();
	}
	
	public Date getSelectedRequestCreateDate() {
		return selectedRequest.getCreateDate();
	}
	
	public Date getSelectedRequestEndDate() {
		return selectedRequest.getEndDate();
	}
	
	
	public Distr getSelectedRequestDistributors() {
		return selectedRequest.getDistributors();
	}

	public String getSelectedRequestProcess() {
		return selectedRequest.getProcess();
	}
	
	public String getSelectedRequestStatus() {
		return selectedRequest.getStatus();
	}

	public void setSelectedRequestId(int id) {
		selectedRequest.setId(id);
	}
	
	public void setSelectedRequestName(String name) {
		selectedRequest.setName(name);
	}
	
	public void setSelectedRequestCustomer(User customer) {
		selectedRequest.setCustomer(customer);
	}
	
	public void setSelectedRequestResponsible(User responsible) {
		selectedRequest.setResponsible(responsible); 
	}
	
	public void setSelectedRequestAccountable(User accountable) {
		selectedRequest.setAccountable(accountable);
	}

	public void setSelectedRequestConsulted(User consulted) {
		selectedRequest.setConsulted(consulted);
	}
	
	public void setSelectedRequestInformed(User informed) {
		selectedRequest.setInformed(informed);
	}
	
	public void setSelectedRequestSupport(User support) {
		selectedRequest.setSupport(support);
	}

	public void setSelectedRequestCreateDate(Date newCreateDate) {
		selectedRequest.setCreateDate(newCreateDate);		
	}

	public void setSelectedRequestEndDate(Date newEndDate) {
		selectedRequest.setEndDate(newEndDate);
	}

	public void setSelectedRequestDistributors(Distr distributors) {
		selectedRequest.setDistributors(distributors);
	}

	public void setSelectedRequestProcess(String process) {
		selectedRequest.setProcess(process);
	}
	
	public void setSelectedRequestStatus(String status) {
		selectedRequest.setStatus(status);
	}
	
}
